<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-21
 * Func   用户提现
 */
defined('IN_YuLin') || exit('NO PERMIT!');

UserLogin(U($m.'/'.$c.'/'.$a));

$table = Table('user_money_cash');
$btable = Table('user_bank');

!$_user['paypassword'] && ShowMsg('请先设置取款密码',U('member/password'));

//	银行卡信息
$banks = $db->getall('SELECT * FROM '.$btable.' WHERE uid = '.$_uid,'id');
!$banks && ShowMsg('请先绑定银行卡',U('member/bank'));


if(IS_POST)
{
	$money = round($_POST['money'],2);
	$bankid = intval($_POST['cardid']);
	$pwd = trim($_POST['pwd']);
	
	$money < 100 && AjaxReturn(1,'取款金额不能低于100');
	$money > 5000000 && AjaxReturn(1,'取款金额不能高于5000000');
	$money > $_user['money'] && AjaxReturn(1,'最多只可以取款'.$money);
	
	!array_key_exists($bankid,$banks) && AjaxReturn(1,'银行卡选择有误');
	
	!$pwd && AjaxReturn(1,'取款密码填写有误');
	(Password($pwd) != $_user['paypassword']) && AjaxReturn(1,'取款密码不正确');
	
	//	取款记录
	$tmp = [];
	$tmp['uid']		=	$_uid;
	$tmp['orderid']	=	date('YmdHis').rand(1000,9999);
	$tmp['money']	=	$money;
	$tmp['bank']	=	$banks[$bankid]['bank'];
	$tmp['bankcard']=	$banks[$bankid]['bankcard'];
	$tmp['truename']=	$banks[$bankid]['truename'];
	$tmp['status']	=	0;
	$tmp['ip']		=	$_Y['ip'];
	$tmp['dateline']=	TIMESTAMP;
	$id = $db->add('user_money_cash',$tmp);
	!$id && AjaxReturn(1,'提现失败，请重新提交');
    
	//	资金日志
    $tmp = [];
	$tmp['uid']		=	$_uid;
	$tmp['balance']	=	-$money;
	$tmp['tid']		=	'cash';
	$tmp['lkey']	=	'cash_'.$_uid.'_'.$id;
	$tmp['money']	=	$money;
	$tmp['notes']	=	'取现申请';
	$res = MoneyLog($tmp);
	if(!$res)
	{
		$db->exec('UPDATE '.$table.' SET dotime = '.TIMESTAMP.',status = 2 WHERE id = '.$id);
		
		AjaxReturn(1,'申请提现失败');
	}
	
	AjaxReturn(0,'申请提现成功，请等待审核');
}

if($a == 'flow')	//	流水
{
	$head['title'] = '查看流水';

	$tpl->display('member/cash_flow');
	exit;
}
else if($a == 'log')	//	记录
{
	$statuss = ['待审核','提现成功','提现失败'];
	
	$data  = [];
	$where = ' FROM '.$table.' t WHERE t.uid = '.$_uid;

	$dt = NoBadStr($_GET['dtrange']);
	$dt = explode(' - ',$dt);
	!$dt[0] && $dt[0] = date('Y-m-d',strtotime('-1 month'));
	!$dt[1] && $dt[1] = date('Y-m-d');
	$dtrange = implode(' - ',$dt);

	$where .= ' AND t.dateline >= '.strtotime($dt[0]).' AND t.dateline < '.(strtotime($dt[1]) + 86400);

	$page = max(1,intval($_GET['page']));
	$size = max(20,intval($_GET['size']));

	$tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);
	if($tmp['p'])
	{
		$orderby = ' ORDER BY t.id DESC ';
		$limit = ' LIMIT '.($page - 1) * $size.','.$size;
		
		$data = $db->getall('SELECT t.* '.$where.$orderby.$limit);
		
		$url = U($m.'/'.$c,['dtrange'=>$dtrange]);
		$multi = Multi($url,$page,$tmp['p'],$size);
	}
	
	$head['title'] = '提款进度';

	$tpl->display('member/cash_log');
	exit;
}

//	银行卡
$combanks = [];
$combanks['中国工商银行']	=	1;
$combanks['中国农业银行']	=	2;
$combanks['中国民生银行']	=	3;
$combanks['中国建设银行']	=	4;
$combanks['中国招商银行']	=	5;
$combanks['中国银行']		=	6;
$combanks['中国交通银行']	=	7;
$combanks['中国邮政银行']	=	8;
$combanks['中国兴业银行']	=	9;
$combanks['华夏银行']		=	10;
$combanks['浦发银行']		=	11;
$combanks['广州银行']		=	12;
$combanks['BEA东亚银行']	=	13;
$combanks['广州农商银行']	=	14;
$combanks['顺德农商银行']	=	15;
$combanks['北京银行']		=	16;
$combanks['平安银行']		=	17;
$combanks['杭州银行']		=	18;
$combanks['温州银行']		=	19;
$combanks['上海农商银行']	=	20;
$combanks['尧都信用社']		=	21;
$combanks['中国光大银行']	=	22;
$combanks['中信银行']		=	23;
$combanks['渤海银行']		=	24;
$combanks['浙商银行']		=	25;
$combanks['晋商银行']		=	26;
$combanks['汉口银行']		=	27;
$combanks['浙江稠州商业银行']=	28;
$combanks['上海银行']		=	29;
$combanks['广发银行']		=	30;
$combanks['东莞银行']		=	32;
$combanks['宁波银行']		=	33;
$combanks['南京银行']		=	34;
$combanks['北京农商银行']	=	36;
$combanks['重庆银行']		=	37;
$combanks['广西信用社']		=	38;
$combanks['江苏银行']		=	40;
$combanks['吉林银行']		=	41;
$combanks['成都银行']		=	42;
$combanks['农村信用社']		=	46;
$combanks['晋城银行']		=	47;
$combanks['邯郸银行']		=	48;
$combanks['郑州银行']		=	49;
$combanks['九江银行']		=	50;
$combanks['财付通']			=	51;
$combanks['盛京银行']		=	52;
$combanks['安徽农村信用合作社']=	53;
$combanks['甘肃银行']		=	54;
$combanks['甘肃农村信用社']	=	55;
$combanks['东莞农村商业银行']=	56;
$combanks['广东农村商业银行']=	57;
$combanks['Payoneer']		=	58;
$combanks['NETELLER ']		=	59;
$combanks['Skrill']			=	60;
$combanks['PayPal']			=	61;
$combanks['其它银行']		=	62;
$combanks['锦州银行']		=	63;
$combanks['齐鲁银行']		=	64;
$combanks['QQ钱包']			=	65;
$combanks['贵阳银行']		=	66;
$combanks['深圳农商银行']	=	67;

$head['title'] = '申请提现';

$tpl->display('member/cash');